#include <algorithm>
#include <stack>
using namespace std;
/*
 * @lc app=leetcode.cn id=98 lang=cpp
 *
 * [98] 验证二叉搜索树
 */


struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
typedef long long ll;
class Solution {
public:
    // bool isValidBST(TreeNode* root) {
    //     return helper(root, INT_MIN, INT_MAX);
    // }

    // 二叉树的特点就是一个范围, 这个范围是递归的很重要因素
    // bool helper(TreeNode* root,  ll l, ll r) {
    //     if (!root) return true;
    //     if (root->val > r || root->val < l) return false;
    //     return helper(root->left, l, min((ll) root->val - 1, r)) && helper(root->right, max((ll) root->val + 1, l), r);
    // }

    ll pre = LLONG_MIN;
    // 采用中序遍历的思路
    bool isValidBST(TreeNode* root) {
        if (!root) return true;
        bool l = isValidBST(root->left);
        if (pre >= root->val) return false;
        pre = root->val;
        bool r = isValidBST(root->right);
        return l && r;
    }

};
// @lc code=end

